//
//  JULoginAndRegistVC.m
//  findFriend
//
//  Created by ju on 14-9-14.
//  Copyright (c) 2014年 dono. All rights reserved.
//

#import "JULoginAndRegistVC.h"
#import <AVOSCloud/AVOSCloud.h>
#import <QuartzCore/QuartzCore.h>
#import "JUAppDelegate.h"
#import "UITextField+UITextFieldCategory.h"
#define JUSwtichLineHeight 30.0f

@interface JULoginAndRegistVC ()<UITextFieldDelegate>
{
    UITextField *_usernameTxt;
    UITextField *_passwdTxt;
    
    UITextField *_usernameTxtR;
    UITextField *_passwdTxtR;
    
    UIScrollView *_scrollView;
    
    UIView *_loginView;
    UIView *_registView;
    
    CGFloat originX ;
    CGFloat originY ;
    CGFloat width ;
    CGFloat height;
}

@end

@implementation JULoginAndRegistVC

- (void)loadView
{
    [super loadView];
    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    
    _scrollView.scrollEnabled = NO;
    _scrollView.directionalLockEnabled = YES;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsVerticalScrollIndicator = NO;
    
    self.view = _scrollView;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupVariable];
    [self setupLoginView];
    [self setupRegistView];
}

#pragma mark 设置登录界面
- (void)setupLoginView
{
    //初始化登录界面
    _loginView = [[UIView alloc] initWithFrame:self.view.bounds];
    
    //设置登录界面的背景
    [self setupBackground:_loginView];
    [self addTapGesture:_loginView];
    
    //初始化用户名框
    _usernameTxt = [[UITextField alloc] initWithFrame:CGRectMake(originX, originY, width, height)];
    _usernameTxt.placeholder = @"用户名";
    _usernameTxt.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _usernameTxt.delegate = self;
    _usernameTxt.returnKeyType = UIReturnKeyNext;
    _usernameTxt.clearButtonMode = UITextFieldViewModeWhileEditing;
    _usernameTxt.backgroundColor = [UIColor whiteColor];
    _usernameTxt.layer.cornerRadius = 4;
    //    [_usernameTxtR drawPlaceholderInRect:CGRectMake(10, 0, _usernameTxtR.frame.size.width-5, _usernameTxtR.frame.size.height)];
    [_usernameTxt clipsToBounds];
    [_loginView addSubview:_usernameTxt];
    
    //初始化密码框
    originY += height+1;
    _passwdTxt = [[UITextField alloc] initWithFrame:CGRectMake(originX, originY, width, height)];
    _passwdTxt.placeholder = @"密码";
    _passwdTxt.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _passwdTxt.delegate = self;
//    _passwdTxt.textColor = [UIColor whiteColor];
    _passwdTxt.returnKeyType = UIReturnKeyNext;
    _passwdTxt.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passwdTxt.backgroundColor = [UIColor whiteColor];
    _passwdTxt.layer.cornerRadius = 4;
    _passwdTxt.secureTextEntry = YES;
    [_passwdTxt clipsToBounds];
    [_loginView addSubview:_passwdTxt];
    
    //初始化登录界面
    originY += height +7;
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame = CGRectMake(originX, originY, width, height);
    [loginBtn setTitle:@"登  录" forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont boldSystemFontOfSize:22];
    loginBtn.titleLabel.textColor = [UIColor whiteColor];
    loginBtn.backgroundColor = [UIColor orangeColor];
    [loginBtn clipsToBounds];
    
    [loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [_loginView addSubview:loginBtn];
    
    //初始化切换到注册界面按钮
    UIButton *swithc2RegistView = [UIButton buttonWithType:UIButtonTypeCustom];
    swithc2RegistView.frame = CGRectMake(0,
                                 CGRectGetHeight([UIScreen mainScreen].bounds)-JUSwtichLineHeight, CGRectGetWidth(self.view.bounds),
                                 JUSwtichLineHeight);
    [swithc2RegistView setImage:[UIImage imageNamed:@"arrawdown"] forState:UIControlStateNormal];
    
    [swithc2RegistView addTarget:self action:@selector(switchToRegistView:) forControlEvents:UIControlEventTouchUpInside];
    [_loginView addSubview:swithc2RegistView];
    
    UILabel *tips = [[UILabel alloc] initWithFrame:CGRectMake(0, swithc2RegistView.frame.origin.y - 30, swithc2RegistView.frame.size.width, 30)];
    tips.backgroundColor = [UIColor clearColor];
    tips.textColor = [UIColor whiteColor];
    tips.textAlignment = NSTextAlignmentCenter;
    tips.text = @"立即注册";
    [_loginView addSubview:tips];
    
    //添加登录界面到_scrollView
    [_scrollView addSubview:_loginView];
}

#pragma mark 登录事件
- (void)login
{
    [AVUser logInWithUsernameInBackground:_usernameTxt.text password:_passwdTxt.text block:^(AVUser *user, NSError *error) {
        if (!error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                JUAppDelegate *delegate = [UIApplication sharedApplication].delegate;
                [delegate showMain];
            });
        }
    }];
}

- (void)switchToRegistView:(id)sender
{
    [_scrollView scrollRectToVisible:_registView.frame animated:YES];
}

#pragma mark 设置注册界面
- (void)setupRegistView
{
    CGRect frame = _loginView.frame;
    frame.origin.y = frame.size.height;
    
    //增加scrollView 的contentSize
    _scrollView.contentSize = CGSizeMake(frame.size.width, frame.size.height * 2);
    
    //初始化_registView
    _registView = [[UIView alloc] initWithFrame:frame];
    
    //设置背景图片和触摸事件
    [self setupBackground:_registView];
    [self addTapGesture:_registView];
    
    //初始化用户名框
    originY = 180;
    _usernameTxtR = [[UITextField alloc] initWithFrame:CGRectMake(originX, originY, width, height)];
    _usernameTxtR.placeholder = @"用户名";
    _usernameTxtR.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _usernameTxtR.delegate = self;
//    _usernameTxtR.textColor = [UIColor whiteColor];
    _usernameTxtR.returnKeyType = UIReturnKeyNext;
    _usernameTxtR.clearButtonMode = UITextFieldViewModeWhileEditing;
    _usernameTxtR.backgroundColor = [UIColor whiteColor];
    _usernameTxtR.layer.cornerRadius = 4;
//    [_usernameTxtR drawPlaceholderInRect:CGRectMake(10, 0, _usernameTxtR.frame.size.width-5, _usernameTxtR.frame.size.height)];
    [_usernameTxtR clipsToBounds];
    [_registView addSubview:_usernameTxtR];
    
    //初始化密码框
    originY += height+1;
    _passwdTxtR = [[UITextField alloc] initWithFrame:CGRectMake(originX, originY, width, height)];
    _passwdTxtR.placeholder = @"密码";
    _passwdTxtR.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _passwdTxtR.delegate = self;
//    _passwdTxtR.textColor = [UIColor whiteColor];
    _passwdTxtR.returnKeyType = UIReturnKeyNext;
    _passwdTxtR.backgroundColor = [UIColor whiteColor];
    _passwdTxtR.layer.cornerRadius = 4;
    [_passwdTxtR clipsToBounds];
    _passwdTxtR.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passwdTxtR.secureTextEntry = YES;
    [_registView addSubview:_passwdTxtR];
    
    //初始化注册按钮
    originY += height +7;
    UIButton *registBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    registBtn.frame = CGRectMake(originX, originY, width, height);
    [registBtn setTitle:@"注  册" forState:UIControlStateNormal];
    registBtn.titleLabel.font = [UIFont boldSystemFontOfSize:22];
    registBtn.titleLabel.textColor = [UIColor whiteColor];
    registBtn.backgroundColor = [UIColor orangeColor];
    [registBtn addTarget:self action:@selector(regist:) forControlEvents:UIControlEventTouchUpInside];
    [_registView addSubview:registBtn];
    
    //初始化切换按钮
    UIButton *switch2LoginView = [UIButton buttonWithType:UIButtonTypeCustom];
    switch2LoginView.frame = CGRectMake(0,
                                 20, CGRectGetWidth(self.view.bounds),
                                 JUSwtichLineHeight);
    [switch2LoginView setImage:[UIImage imageNamed:@"arrawup"] forState:UIControlStateNormal];
//    [switch2LoginView setImage:[UIImage imageNamed:@"arrawdownPressed"] forState:UIControlStateHighlighted];
    
    [switch2LoginView addTarget:self action:@selector(switchToLoginView:) forControlEvents:UIControlEventTouchUpInside];
    [_registView addSubview:switch2LoginView];
    
    //把注册界面添加到_scrollView
    [_scrollView addSubview:_registView];
}

- (void)switchToLoginView:(id)sender
{
    [_scrollView scrollRectToVisible:_loginView.frame animated:YES];
}

#pragma mark 注册
- (void)regist:(id)sender
{
    //加校验
    AVUser *user = [[AVUser alloc] init];
    user.username = _usernameTxtR.text;
    user.password = _passwdTxtR.text;
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            dispatch_async(dispatch_get_main_queue(), ^{
                JUAppDelegate *delegate = [UIApplication sharedApplication].delegate;
                [delegate showMain];
            });
        }else{
            NSLog(@"注册失败，提示错误");
        }
    }];
}

#pragma mark 设置背景图片
- (void)setupBackground:(UIView *)view
{
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    bgView.image = [UIImage imageNamed:@"background"];
    [view addSubview:bgView];
}

#pragma mark 添加触摸事件
- (void)addTapGesture:(UIView *)view;
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    
    [self.view addGestureRecognizer:tap];
}

- (void)tap:(id)sender
{
    [_scrollView endEditing:YES];
}

#pragma mark 初始化变量
- (void)setupVariable
{
    originX = 30;
    originY = 180;
    width = self.view.frame.size.width - originX * 2;
    height = 40;
}




@end
